<?php

class Application_Model_Dao_Year extends Zend_Db_Table_Abstract
{

	protected $_name = 'years';
    protected $_primary = 'id';
    	
	public function init(){
    	
    }
	public function getAllYear(){
    	$dbAdapter = Zend_Db_Table::getDefaultAdapter();
    	$select = $dbAdapter->select()->from('years')->joinLeft('faculties', 'years.faculty_id = faculties.id', array('faculty_designation' => 'designation')) 
    												 ->joinLeft('majors', 'years.major_id = majors.id', array('major_designation' => 'designation'))
    												 ->joinLeft('studies_types', 'years.studies_id = studies_types.id', array('typeStudy_designation' => 'designation'))
    												 ->joinLeft('studies_degrees', 'years.tutor_id = studies_degrees.id', array('degreeStudy_designation' => 'designation'));
		$result = $dbAdapter->fetchAll($select);
		return $result;
    }
public function getRowYear($id){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('years')->where('years.id = \''.$id.'\'')->joinLeft('faculties', 'years.faculty_id = faculties.id', array('faculty_designation' => 'designation'))
																							    ->joinLeft('majors', 'years.major_id = majors.id', array('major_designation' => 'designation'))
    												 										    ->joinLeft('studies_types', 'years.studies_id = studies_types.id', array('typeStudy_designation' => 'designation'))
    												 											->joinLeft('studies_degrees', 'years.tutor_id = studies_degrees.id', array('degreeStudy_designation' => 'designation'));
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
 	public function getAll(){
		$result = $this->fetchAll();
		if( !$result )
			throw new Exception("Wystąpił błąd przy pobieraniu danych z bazy. Proszę spróbować jeszcze raz. Jeśli problem będzie się powtarzać, prosimy skontaktować się z administratorem.");	
		return $result;
    } 
	public function getYearByID($id){
		$id = (int)$id;
		$row = $this->fetchRow('id = '.$id);
		if (!$row)
			throw new Exception("Rok o ID $id nie istnieje.");
		return $row->toArray();
	}


	public function saveYear($year) {
    	$data = array(
			'faculty_id' => $year->getFaculty(),
    		'major_id' => $year->getMajor(),
			'numberStudentYear' => $year->getNumberStudentYear(),
    		'numberGroupLecture' => $year->getNumberGroupLecture(),
    		'abundanceGroup' => $year->getAbundanceGroup(),
    		'typeStudy_id' => $year->getTypeStudy(),
    		'degreeStudy_id' => $year->getDegreeStudy(),
		);
		$this->insert($data);
     return ;
    }
public function updateYear($year) {
		$data = array(
			'faculty_id' => $year->getFaculty(),
    		'major_id' => $year->getMajor(),
			'numberStudentYear' => $year->getNumberStudentYear(),
    		'numberGroupLecture' => $year->getNumberGroupLecture(),
    		'abundanceGroup' => $year->getAbundanceGroup(),
    		'typeStudy_id' => $year->getTypeStudy(),
    		'degreeStudy_id' => $year->getDegreeStudy(),
		);
		$this->update($data, 'id = '. (int)$year->getId());
		return ;
		}
		
public function deleteYear($year){
		$this->delete('id =' . (int)$year->getId());
		return;
	}
		
public function getToSelect(){
    	//$array = $this->getAll()->toArray();
    	$studiesDao = new Application_Model_Dao_Studies();
  		$degreeStudies = $studiesDao->getToSelect();
    	$years[''] = "wybierz";
    	//foreach ($array as $option) {
     	//$years[ $option['id'] ] = $option['degreeStudy_id' ] ;
     //	}
     return $degreeStudies;
    }
}